﻿using ProjectClass.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectClass.ShopingCart
{
    public class CartItem : IEquatable<CartItem>
    {
        ProjectClassEntities db = new ProjectClassEntities();
        public CartItem(int productId)
        {
            this.ProductId = productId;
        }
        // A place to store the quantity in the cart
        // This property has an implicit getter and setter.
        public int Quantity { get; set; }
        private int _productId;
        private tbl_Product _product = null;
        public int ProductId
        {
            get { return _productId; }
            set
            {
                // To ensure that the Prod object will be re-created
                _productId = value;
            }
        }
        public tbl_Product Prod
        {

            get
            {
                // Lazy initialization - the object won't be created until it is needed
                if (_product == null)
                {
                    _product = db.tbl_Product.Find(ProductId);
                }
                return _product;
            }
        }
        public int ProductID
        {
            get { return Prod.ProductId; }
        }
        public string Name
        {
            get { return Prod.Name; }
        }
        public string Avatar
        {
            get { return Prod.Avatar; }
        }
        public decimal Price
        {

            get
            {
                
                    return Prod.Price;
            }
        }
        public decimal TongCong
        {
            get
            { 
                    return Prod.Price * Quantity;
            }

        }
        /**
	 * Equals() - Needed to implement the IEquatable interface
	 *    Tests whether or not this item is equal to the parameter
	 *    This method is called by the Contains() method in the List class
	 *    We used this Contains() method in the ShoppingCart AddItem() method
	 */
        public bool Equals(CartItem item)
        {
            return item.ProductId == this.ProductId;
        }
    }
}
